package com.example.statemachine.util;

import lombok.Data;

import java.io.Serializable;

/**
 * @author jianwei.wang
 * @version v1.0
 * @date 2019/5/15 18:11
 **/
@Data
public class MessageBody<T> implements Serializable {

    private static final long serialVersionUID = 4333018446468131847L;

    private int code;

    private String message;

    private T data;

    private boolean success;

    private static int SUCCESS_CODE = 10000;
    private static int ERROR_CODE = 10001;

    public MessageBody(int code, String message, boolean success) {
        this.code = code;
        this.message = message;
        this.success = success;
    }

    public static MessageBody success(){
        return new MessageBody(SUCCESS_CODE,"success",true);
    }
    public static MessageBody success(Object data){
        MessageBody messageBody = new MessageBody(SUCCESS_CODE,"success",true);
        messageBody.setData(data);
        return messageBody;
    }
    public static MessageBody error(String message){
        return new MessageBody(ERROR_CODE,message,false);
    }
    public static MessageBody error(BusinessException e){
        return new MessageBody(e.getCode(),e.getMessage(),false);
    }
}
